﻿using System;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.Helpers;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Modules.GUIDE.Components;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using ASPNETVN.PORTAL.Components.Share;

namespace ASPNETVN.PORTAL.Modules.GUIDE.Controls
{
    public partial class GuideContentManager : AddEditItemControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {                
                LoadData();
            }
        }
        public void LoadData()
        {
            //bind gridview
            DataSet dt = Components.Article.SelectArticles_ByModuleID(this.ModuleID, 0, 0,"%%");
            gvGuideContent.DataSource = dt;
            gvGuideContent.DataBind();

            //bind dropdown
            ASPNETVN.PORTAL.Components.Share.Category.BuildDropDownListControl(ref cbCategory, this.ModuleID);
            cbCategory.Items.Insert(0, new ListItem("---Chọn lĩnh vực---","0"));
            cbGuider.DataSource = Components.Guider.SelectAll(this.ModuleID);
            cbGuider.DataBind();
            cbGuider.Items.Insert(0, new ListItem("---Chọn cơ quan thực hiện---", "0"));
        }

        protected void gvGuideContent_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                Response.Redirect(HttpUrlBuilder.BuildAddEditUrl(this.ModuleID, Convert.ToInt32(e.CommandArgument), "AddEditItem"));
            }
            if (e.CommandName == "Delete")
            {
                //Goi ham xoa
                
            }
        }

        protected void btnSearchArea_Click(object sender, EventArgs e)
        {
            string value = txtTitle.Text.Trim();
            if (txtTitle.Text.IndexOf("%") < 0)
            {
                gvGuideContent.DataSource = Components.Article.SelectArticles_ByModuleID(this.ModuleID, Int32.Parse(cbCategory.SelectedValue), 0, "%"+value+"%");
                gvGuideContent.DataBind();
                if (Int32.Parse(cbGuider.SelectedValue) != 0)
                {
                    cbGuider.SelectedValue = "0";
                }
            }
            else
            {
                this.ShowMessage("Thông báo", "Không chấp nhận các ký tự đặc biệt");
            }
            
        }

        protected void gvGuideContent_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Components.Article.DeleteArticle(Convert.ToInt32(gvGuideContent.DataKeys[e.RowIndex].Value));
            LoadData();
        }

        protected void btnSearchGuider_Click(object sender, EventArgs e)
        {
            string value = txtTitle.Text.Trim();
            if (txtTitle.Text.IndexOf("%") < 0)
            {
                gvGuideContent.DataSource = Components.Article.SelectArticles_ByModuleID(this.ModuleID, 0, Int32.Parse(cbGuider.SelectedValue), "%" + value + "%");
                gvGuideContent.DataBind();
                if (Int32.Parse(cbCategory.SelectedValue) != 0)
                {
                    cbCategory.SelectedValue = "0";
                }
            }
            else
            {
                this.ShowMessage("Thông báo", "Không chấp nhận các ký tự đặc biệt");
            }           
        }


        protected void gvGuideContent_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvGuideContent.PageIndex = e.NewPageIndex;
            LoadData();
        }
    }
}